package com.ticket.sass.ticketserver.interceptor

import com.ticket.sass.ticketserver.dto.output.ResultWrapOutput
import com.ticket.sass.ticketserver.exception.BadRequestException
import mu.KotlinLogging
import org.springframework.web.method.HandlerMethod
import org.springframework.web.servlet.HandlerInterceptor
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


class RequestInterceptor : HandlerInterceptor {
    private val logger = KotlinLogging.logger {}

    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
        if (handler !is HandlerMethod) return true

        val config = request.getHeader("Config")
            ?: throw BadRequestException(ResultWrapOutput.ErrorCode.BAD_REQUEST, "Header 缺少 Config 参数")

        val platform = request.getHeader("Platform")
            ?: throw BadRequestException(ResultWrapOutput.ErrorCode.BAD_REQUEST, "Header 缺少 Platform 参数")

        request.setAttribute("platform", platform)
        request.setAttribute("config", config)

        return true
    }
}